package com.wdf.rule.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.gitee.starblues.core.PluginInfo;
import com.gitee.starblues.integration.operator.PluginOperator;
import com.gitee.starblues.integration.operator.upload.UploadByMultipartFileParam;
import com.wdf.rule.drools.DynamicTest;
import com.wdf.rule.drools.FuRuleKieBuilderImpl;
import com.wdf.rule.drools.FuRuleProjectType;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.Charset;

/**
 * @author wangdingfu
 * @date 2023-11-16 18:01:49
 */
@RestController
@RequestMapping("/plugin")
@RequiredArgsConstructor
public class PluginController {


    private final PluginOperator pluginOperator;


    @PostMapping("upload")
    public PluginInfo uploadPlugin(MultipartFile file) {
        return pluginOperator.uploadPlugin(new UploadByMultipartFileParam(file));
    }


    @PostMapping("executeRule")
    public String executeRule(@RequestBody RuleParam ruleParam) {
        Class<?> aClass = DynamicTest.orderTestClass();
        Object bean = JSONUtil.toBean(ruleParam.getRuleData(), aClass);
        KieServices kieServices = KieServices.get();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write("src/main/resources/rules/order_rule.drl", StringUtils.getBytes(ruleParam.getRuleContent(), Charset.defaultCharset()));
        FuRuleKieBuilderImpl fuRuleKieBuilder = new FuRuleKieBuilderImpl(kieFileSystem, aClass.getClassLoader());
        fuRuleKieBuilder.buildAll(FuRuleProjectType.class);

        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(bean);
        kieSession.fireAllRules();
        kieSession.dispose();
        return "执行成功";
    }


    public void test() {
//        Object bean = JSONUtil.toBean(ruleParam.getRuleData(), beanClass);
        KieHelper helper = new KieHelper();
        Resource resource = ResourceFactory.newByteArrayResource(FileUtil.readBytes("/Users/wangdingfu/fu-rule-example/classes/OrderInfo.class"));
        resource.setResourceType(ResourceType.JAVA);
        resource.setSourcePath("src/main/java/com/wdf/rule/pojo");
        helper.addResource(resource);
        helper.addContent("ruleParam.getRuleContent()", ResourceType.DRL);
        KieSession kSession = helper.build().newKieSession();
    }

}
